<?php

namespace app\http\middleware;

use app\models\{Store as model};

use think\Db;

class Store
{
    public function handle($request, \Closure $next)
    {
        $id = session('store_id');
        $store = (new model())->where('id', $id)->find();
        $url = $request->baseUrl();
        if (!$store && $url != '/index/login') {
            return redirect('/index/login');
        }
        if ($store && $url == '/') {
            return redirect('/main/index');
        }
        if ($store['status'] == 2) {
            session('store_id', null);
            return redirect('/index/login');
        }
        $request->store = $store;
        return $next($request);
    }
}
